/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FlightDialogImpl.java
 *
 * Created on 2010-10-6, 8:27:55
 */
package ATBMS.view.manager;

import ATBMS.control.MainApp;
import ATBMS.modles.Flight;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Felix
 */
public class FlightDialogImpl extends javax.swing.JDialog implements FlightDialogInterface {

    private boolean clickOK = false;

    /** Creates new form FlightDialogImpl */
    public FlightDialogImpl(java.awt.Frame parent) {
        super(parent, true);
        initComponents();
        rootPane.setDefaultButton(okButton);

    }

    public Flight doModal() {
        MainApp.getApplication().show(this);
        if (clickOK) {
            Date gooff;
            Date arrival;
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");

            try {
                gooff = df.parse(gooffTextField.getText().trim());
                arrival = df.parse(arrivalTextField.getText().trim());
            } catch (Exception ex) {
                return null;
            }

            Flight flight = new Flight(flightTextField.getText().trim(),
                    destTextField.getText().trim(),
                    airlineTextField.getText().trim(),
                    gooff,
                    arrival,
                    Integer.valueOf(busloadTextField.getText().trim()),
                    equipTextField.getText().trim(),
                    Float.valueOf(priceTextField.getText().trim()));
            return flight;
        }
        return null;
    }

    private boolean checkInput() {

        if (destTextField.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "Destnation can't be null");
            return false;
        }

        if (flightTextField.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "Flight can't be null");
            return false;
        }

        if (airlineTextField.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "Ariline can't be null");
            return false;
        }

        if (busloadTextField.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "Busload can't be null");
            return false;
        }

        try {
            Integer.parseInt(busloadTextField.getText().trim());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Busload must be number");
            return false;
        }

        if (equipTextField.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "Equip can't be null");
            return false;
        }

        if (priceTextField.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "Price can't be null");
            return false;
        }
        try {
            Float.valueOf(priceTextField.getText().trim());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Price must be number");
            return false;
        }
        Date gooff;
        Date arrival;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");

        try {
            gooff = df.parse(gooffTextField.getText().trim());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Go-Off must be a valid time format");
            return false;
        }

        try {
            arrival = df.parse(arrivalTextField.getText().trim());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Arrival must be a valid time format");
            return false;
        }


        return true;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        destTextField = new javax.swing.JTextField();
        airlineTextField = new javax.swing.JTextField();
        flightTextField = new javax.swing.JTextField();
        busloadTextField = new javax.swing.JTextField();
        equipTextField = new javax.swing.JTextField();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        gooffTextField = new javax.swing.JTextField();
        arrivalTextField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        priceTextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ATBMS.control.MainApp.class).getContext().getResourceMap(FlightDialogImpl.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        destTextField.setText(resourceMap.getString("destTextField.text")); // NOI18N
        destTextField.setName("destTextField"); // NOI18N

        airlineTextField.setText(resourceMap.getString("airlineTextField.text")); // NOI18N
        airlineTextField.setName("airlineTextField"); // NOI18N

        flightTextField.setText(resourceMap.getString("flightTextField.text")); // NOI18N
        flightTextField.setName("flightTextField"); // NOI18N

        busloadTextField.setText(resourceMap.getString("busloadTextField.text")); // NOI18N
        busloadTextField.setName("busloadTextField"); // NOI18N

        equipTextField.setText(resourceMap.getString("equipTextField.text")); // NOI18N
        equipTextField.setName("equipTextField"); // NOI18N

        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        okButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                okButtonMouseClicked(evt);
            }
        });

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelButtonMouseClicked(evt);
            }
        });

        gooffTextField.setText(resourceMap.getString("gooffTextField.text")); // NOI18N
        gooffTextField.setName("gooffTextField"); // NOI18N

        arrivalTextField.setText(resourceMap.getString("arrivalTextField.text")); // NOI18N
        arrivalTextField.setName("arrivalTextField"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        priceTextField.setText(resourceMap.getString("priceTextField.text")); // NOI18N
        priceTextField.setName("priceTextField"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel8)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addGap(50, 50, 50)
                        .addComponent(cancelButton))
                    .addComponent(equipTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(busloadTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(airlineTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(flightTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(destTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(gooffTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(arrivalTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(priceTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(destTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(flightTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(airlineTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(gooffTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(arrivalTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(busloadTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(equipTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(priceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_okButtonMouseClicked
        if (checkInput()) {
            clickOK = true;
            dispose();
        }
    }//GEN-LAST:event_okButtonMouseClicked

    private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonMouseClicked
        dispose();
    }//GEN-LAST:event_cancelButtonMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JTextField airlineTextField;
    protected javax.swing.JTextField arrivalTextField;
    protected javax.swing.JTextField busloadTextField;
    private javax.swing.JButton cancelButton;
    protected javax.swing.JTextField destTextField;
    protected javax.swing.JTextField equipTextField;
    protected javax.swing.JTextField flightTextField;
    protected javax.swing.JTextField gooffTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JButton okButton;
    protected javax.swing.JTextField priceTextField;
    // End of variables declaration//GEN-END:variables
}
